﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;

namespace He.Web.manage.settings
{
    public partial class category_edit : He.Web.UI.Manage
    {
        private string action = Enums.ActionEnum.Add.ToString();     //操作类型
        protected int id = 0;
        protected int category_id;                                    //类别ID
        private int channel_id;                                //栏目ID
        protected int parentid;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                    return;
                }
            }
            else
            {
                if (!int.TryParse(Request.QueryString["channel_id"], out channel_id))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您的参数不正确。", "back", "Error");
                    return;
                }
                if (!int.TryParse(Request.QueryString["parentid"], out parentid))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您的参数不正确。", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                if (action == Enums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
                else
                {
                    if (parentid == 0)
                    {
                        lblPname.Text = "顶级类别";
                    }
                    else
                    {
                        lblPname.Text = new He.BLL.category().GetModel(parentid).title;
                    }
                }
            }


        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            He.Model.category model = new He.BLL.category().GetModel(_id);

            int parentid = model.parent_id;

            if (parentid == 0)
            {
                lblPname.Text = "顶级类别";
            }
            else
            {
                lblPname.Text = new He.BLL.category().GetModel(parentid).title;
            }
            txtTitle.Text = model.title;
            txtSortId.Text = model.sort_id.ToString();
            chkProtected.Checked = model.is_protected == 1 ? true : false;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            int layer = 1;
            He.Model.category model = new He.Model.category();
            model.title = txtTitle.Text.Trim();
            model.parent_id = parentid;
            model.id_list = "";//待定
            model.layer = layer;//待定
            model.sort_id = Convert.ToInt32(txtSortId.Text.Trim());
            model.is_protected = chkProtected.Checked ? 1 : 0;
            model.content = "";
            model.add_time = DateTime.Now.ToString();
            model.channel_id = channel_id;
            //插入模块并获取本条记录ID
            int thisId = new He.BLL.category().Add(model);
            //获取新插入数据行的id_list
            if (parentid > 0)
            {
                model.id_list = new He.BLL.category().GetModel(parentid).id_list + thisId.ToString() + ",";//父类id_list加自身ID
                model.layer = new He.BLL.category().GetModel(parentid).layer + 1;
            }
            else
            {
                model.id_list = "," + thisId.ToString() + ",";
                model.layer = 1;
            }
            model.id = thisId;
            //更新id_list及layer
            if (new He.BLL.category().Update(model))
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            Model.category model = new BLL.category().GetModel(_id);
            model.title = txtTitle.Text.Trim();
            model.sort_id = Convert.ToInt32(txtSortId.Text.Trim());
            model.add_time = DateTime.Now.ToString();
            model.is_protected = chkProtected.Checked ? 1 : 0;
            if (!new BLL.category().Update(model))
            {
                JscriptPrint("保存过程中发生错误啦！", "category_list.aspx?channel_id=" + model.channel_id, "Error");
                return false;
            }
            JscriptPrint("修改信息成功啦！", "category_list.aspx?channel_id=" + model.channel_id, "Success");
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                DoEdit(this.id);  
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", "category_list.aspx?channel_id=" + channel_id, "Error");
                    return;
                }
                JscriptPrint("添加信息成功啦！", "category_list.aspx?channel_id=" + channel_id, "Success");
            }
        }
    }
}